<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'GeneralController.php';
class ClientController extends GeneralController {

        /*
         * Index page
         */
        public function index()
	{
	}
        
        public function ajaxConnexion()
	{
            $result=array("mail"=>"","codeErreur"=>null);
            if(isset($_POST["mail"],$_POST["passwordConnexion"])){
                $this->load->database();
                $this->load->model('Client');
                $this->load->helper('url');
                $client = $this->Client->connexion($_POST["mail"], $_POST["passwordConnexion"]); 
                if($client){
                    $result["mail"]=$this->updateSession($client); // renvoi le mail (qui fait office de pseudo utilisateur)
                }else{
                    $result["codeErreur"]=1;//  code erreur: identifiant ou mot de passe invalide 
                }          
                
            }else{
                $result["codeErreur"]=2; // code des informations manquantes
            }
            echo json_encode($result);
	}
        
        public function ajaxInscription()
	{
            $result=array("mail"=>"","codeErreur"=>null);
            if(isset($_POST["nom"],$_POST["prenom"],$_POST["adresse"],$_POST["mail"],$_POST["passwordInscription"])){
                $this->load->database();
                $this->load->model('Client');
                $this->load->helper('url');
                if( ! $this->Client->existMail($_POST["mail"]) ){
                    $client = $this->Client->save($_POST["nom"], $_POST["prenom"], $_POST["adresse"], $_POST["mail"], $_POST["passwordInscription"]); 
                   if($client){
                        $result["mail"]=$this->updateSession($client); // renvoi le mail (qui fait office de pseudo utilisateur)
                   }else{
                        $result["codeErreur"]=3;//  code erreur: requête 
                   }
                    
                }else{
                    $result["codeErreur"]=1; // code erreur mail déjà existant
                }
            }else{
                $result["codeErreur"]=2; // code erreur des informations manquantes
            }
            echo json_encode($result);
	}
        
        private function updateSession($client){
                $this->load->library('session');
                $infoClient = array(
                   'mail'     => $client->mail,
                   'id' => $client->id,
                   'logged_in' => TRUE
               );
               $this->session->set_userdata($infoClient);
               return $this->session->userdata('mail');
        }
        public function ajaxInfoUser(){
            $this->load->library('session');
            $this->load->database();
            $this->load->model('Client');
            //var_dump($this->session->userdata('id'));
            $c= $this->Client->findClient($this->session->userdata('id'));
            if($c){$c->password=null;}
            echo json_encode($c);
        }
        public function deconnexion(){
           $this->load->helper('url');
           $this->load->library('session');
           $this->session->sess_destroy();
           redirect(base_url());
        }
        
        
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */